// components/address/address.js
// pages/address/address.js
var cityDataList = require('../../data/cityList').cityList;

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    showArea: {
      type: Boolean,
      value: false, 
      // observer: function (newVal) {
      //   if (newVal) {
      //     this.getSZAreaList()
      //   }
      // }
    },

    countyDTO: {
      type: Object,
      value: {},
    },

    addressDTO: {
      type: Object,
      value: {},
      observer: function (newVal) {
        if (newVal) {
          this.selectAddress()
        }
      }
    },

    currentTab: {
      type: Number,
      value: 1
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    showArea: false,
    loading: false,
    addressDTO: {},
    provinceList: [],
    cityList: [],
    countyList: [],
    countyDTO: {}
  },

  /**
   * 组件的方法列表
   */
  methods: {

    touchmove() {},

    // 得到户口地址数据
    getAreaList(type) {
      if (type == '省') {
        let provinceList = [];
        cityDataList.forEach((item) => {
          provinceList.push({
            name: item.name
          });
        });

        this.setData({
          currentTab: 1,
          provinceList: provinceList,
        });
      } else if (type == '市') {
        let cityList = [];
        cityDataList.forEach((item) => {
          if (item.name == this.data.provinceDTO.name) {
            item.city.forEach((item, index) => {
              cityList.push({
                name: item.name
              });
            });
          }
        });

        this.setData({
          currentTab: 2,
          cityList: cityList,
        });
      } else if (type == '区') {
        let countyList = [];
        cityDataList.forEach((item) => {
          if (item.name == this.data.provinceDTO.name) {
            item.city.forEach((item, index) => {
              if (item.name == this.data.cityDTO.name) {
                item.area.forEach((item) => {
                  countyList.push({
                    name: item.name
                  });
                });
              }
            });
          }
        });

        this.setData({
          currentTab: 3,
          countyList: countyList,
        });
      }
    },

    // 得到广东地址数据
    getGDAreaList() {

      let cityList = [];
      cityDataList.forEach((item) => {
        if (item.name == '广东') {
          item.city.forEach((item, index) => {
            cityList.push({
              name: item.name
            });
          });
        }
      });

      var provinceDTO = {
        "name": "广东"
      }
      this.setData({
        provinceDTO: provinceDTO,
        currentTab: 2,
        cityList: cityList,
      });
    },

    // 得到深圳地址数据
    getSZAreaList() {
      let countyList = [];
      cityDataList.forEach((item) => {
        if (item.name == '广东') {
          item.city.forEach((item, index) => {
            if (item.name == '深圳市') {
              item.area.forEach((item) => {
                countyList.push({
                  name: item.name
                });
              });
            }
          });
        }
      });

      var provinceDTO = {
        "name": "广东"
      }
      var cityDTO = {
        "name": "深圳市"
      }
      this.setData({
        provinceDTO: provinceDTO,
        cityDTO: cityDTO,
        currentTab: 3,
        countyList: countyList,
      });

    },

    areaClose: function () {
      this.setData({
        showArea: false
      });
    },

    //点击省选项卡
    resideProvince: function (e) {
      this.getAreaList("省")
    },
    //点击市选项卡
    resideCity: function () {
      this.getAreaList("市")
    },
    resideCountry: function () {
      this.getAreaList("区")
    },

    // 显示省市区地址信息
    selectAddress: function (e) {
      let addressDTO = this.data.addressDTO;
      if (addressDTO.showArea) { // 当显示地址选择时
        this.setData({
          addressType: addressDTO.addressType
        })
        // this.data.addressType = addressDTO.addressType
        switch (addressDTO.addressType) { // 进行地址类型筛选
          case 1: // 无限制选择
            if (addressDTO.countyDTO && addressDTO.countyDTO.name) {
              this.setData({
                provinceDTO: addressDTO.provinceDTO,
                cityDTO: addressDTO.cityDTO,
                countyDTO: addressDTO.countyDTO
              })
              this.getAreaList("区")
            } else if (addressDTO.cityDTO && addressDTO.cityDTO.name) {
              this.setData({
                provinceDTO: addressDTO.provinceDTO,
                cityDTO: addressDTO.cityDTO
              })
              this.getAreaList("市")
            } else if (addressDTO.provinceDTO && addressDTO.provinceDTO.name) {
              this.setData({
                provinceDTO: addressDTO.provinceDTO
              })
              this.getAreaList("省")
            } else {
              this.setData({
                provinceDTO: {}
              })
              this.getAreaList("省")
            }
            break;

          case 2: // 限制广东
            if (addressDTO.countyDTO && addressDTO.countyDTO.name) {
              this.setData({
                provinceDTO: addressDTO.provinceDTO,
                cityDTO: addressDTO.cityDTO,
                countyDTO: addressDTO.countyDTO
              })
              this.getAreaList("区")
            } else {
              this.getGDAreaList()
            }
            break;

          case 3: // 限制深圳

            if (addressDTO.countyDTO) {
              this.setData({
                countyDTO: addressDTO.countyDTO
              })
            }
            this.getSZAreaList()
            break;
        }

      }
    },

    //点击选择省
    selectProvinceList: function (e) {
      let provinceDTO = e.currentTarget.dataset.item;
      this.setData({
        provinceDTO: provinceDTO,
        cityDTO: {},
        countyDTO: {}
      });
      this.getAreaList("市")
    },

    //点击选择市
    selectCityList: function (e) {
      let cityDTO = e.currentTarget.dataset.item;
      this.setData({
        cityDTO: cityDTO,
        countyDTO: {}
      });
      this.getAreaList("区")
    },
    //点击选择区
    selectCountyList: function (e) {
      var type = e.currentTarget.dataset.type;
      if (type == 0) {
        this.data.countyDTO = e.currentTarget.dataset.item;
        var address = {}
        address.provinceDTO = this.data.provinceDTO;
        address.cityDTO = this.data.cityDTO;
        address.countyDTO = {};
        address.addressType = this.data.addressType
        this.setData({
          showArea: false
        });
        this.triggerEvent('suer', address)
      } else {
        this.data.countyDTO = e.currentTarget.dataset.item;
        var address = {}
        address.provinceDTO = this.data.provinceDTO;
        address.cityDTO = this.data.cityDTO;
        address.countyDTO = this.data.countyDTO;
        address.addressType = this.data.addressType
        this.setData({
          showArea: false
        });
        this.triggerEvent('suer', address)
      }

    },
    // 滑动切换tab
    bindChange: function (e) {
      var that = this;
      that.setData({
        currentTab: e.detail.current + 1
      });
    }
  }
})